<template>
     <div class="login-container">
    <el-card class="login-form">
      <div class="form-header">
        <h2 class="form-title">欢迎登录</h2>
        <p class="form-subtitle">请输入您的账号信息</p>
      </div>

      <el-form :model="form" :rules="rules" @submit.prevent="handleSubmit">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            class="login-button"
            size="large"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import api from '@/assets/js/api.js';
import { useRouter } from 'vue-router'

const router = useRouter()

const form = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
  ]
}

const handleSubmit =async () =>{
    const result = await api.employeeLogin(form);
    if (result.code === 200) {
        localStorage.setItem('userId', result.data.id)
        // 登录成功，跳转到后台管理页面
        router.push('/home').then(() => {
            // 页面跳转后刷新
            window.location.reload();
        });
    } else {
        // 登录失败，显示错误信息
        this.$message.error(result.message || '登录失败，请重试');
    }
}

</script>

<style scoped>
@import'../../assets/css/backLogin.css';
</style>